package com.hzdl.car.contorlle;

import com.github.pagehelper.PageInfo;
import com.hzdl.car.bean.Customer;
import com.hzdl.car.bean.News;
import com.hzdl.car.bean.Rent;
import com.hzdl.car.service.CarService;
import com.hzdl.car.service.RentService;
import com.hzdl.car.util.Common;
import com.hzdl.car.util.HzdlResultJson;
import com.hzdl.car.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.ByteArrayOutputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("rent")
public class RentControlle {
    @Autowired
    private RentService service;

    @Autowired
    private  CarService carService;

    @RequestMapping("init")
    public HzdlResultJson newsInit(RentVo rentVo){
            PageInfo<Rent> pageInfo = service.findWhere(rentVo);
            return new HzdlResultJson(pageInfo.getTotal(),pageInfo.getList());
    }

    @RequestMapping("add")
    public HzdlResultJson add(RentVo rentVo, CarVo carVo){
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("YYYY-MM-dd HH:mm:ss");
        try {
            String returndate1 = rentVo.getReturndate1();
            if (returndate1!=null&&!returndate1.isEmpty()){
                Date parse1 = simpleDateFormat.parse(returndate1);
                rentVo.setReturndate(parse1);
            }else {
                rentVo.setReturndate(null);
            }

            Date parse = simpleDateFormat.parse(rentVo.getBegindate1());
            rentVo.setBegindate(parse);

            int row = service.add(rentVo);
            if (row>0){
                 carService.isrentingupdate(carVo);
                return new HzdlResultJson("添加成功",null);
            }

        } catch (ParseException e) {
            e.printStackTrace();
        }
        return new HzdlResultJson(-1,"添加失败");
    }

    @RequestMapping("update")
    public HzdlResultJson update(RentVo rentVo){
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("YYYY-MM-dd HH:mm:ss");


        try {
            String returndate1 = rentVo.getReturndate1();
            if (returndate1!=null&&!returndate1.isEmpty()){
                Date parse = simpleDateFormat.parse(returndate1);
                rentVo.setReturndate(parse);
            }else {
                rentVo.setReturndate(null);
            }
            int row = service.update(rentVo);
            if (row>0){
                return new HzdlResultJson("修改成功",null);
            }

        } catch (ParseException e) {
            e.printStackTrace();
        }


        return new HzdlResultJson(-1,"修改失败");
    }

    @RequestMapping("delete")
    public HzdlResultJson delete ( String rentid){
        int row = service.delete(rentid);
        if (row>0){
            return HzdlResultJson.DELETE_SUCCESS();
        }else{
            return HzdlResultJson.DELETE_FAIL();
        }
    }
    @RequestMapping("print")
    public ResponseEntity<Object> print(RentVo rentVo){
        Rent rent = service.findByIdrent(rentVo.getRentid());
        Customer customer = service.findById(rent.getIdentity());
        ByteArrayOutputStream outputStream = Common.print2(rent,customer.getCustname());

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
        headers.setContentDispositionFormData("attachment","customers.xls");

        return new ResponseEntity<Object>(outputStream.toByteArray() ,headers , HttpStatus.CREATED);
    }

}
